#include "CZombieRojo.h"

//=============================================================================
CZombieRojo::CZombieRojo() {
    OldTime=0;
}
//=============================================================================
void CZombieRojo::ObtenerDireccion(){
    int opcion=rand() % 5;
    switch (opcion){
        case 0:
            MoveLeft=true;
            MoveRight=false;
            MoveUp=false;
            MoveDown=false;
            break;
        case 1:
            MoveRight=true;
            MoveLeft=false;
            MoveUp=false;
            MoveDown=false;
            break;
        case 3:
            MoveUp=true;
            MoveRight=false;
            MoveLeft=false;
            MoveDown=false;
            break;
        case 4:
            MoveDown=true;
            MoveRight=false;
            MoveUp=false;
            MoveLeft=false;
            break;
    }
}
//-----------------------------------------------------------------------------
void CZombieRojo::OnLoop() {

    //cada X segundos se fija si cambia de direccion o no
    if (OldTime + 3000 < SDL_GetTicks()){
        int cambioDireccion=0;
        OldTime =SDL_GetTicks();
        cambioDireccion=rand() % 2;
        if (cambioDireccion!=0) {
            SpeedX=0;
            SpeedY=0;
            this->ObtenerDireccion();
        }
    }
	if(MoveLeft) {
		AccelX = -0.25;// -1.5;
		AccelY = 0;// -1.5;
	}else
	if(MoveRight) {
		AccelX = 0.25;// 1.5;
		AccelY = 0;// -1.5;
	}
    else
    if(MoveUp) {
		AccelY = -0.25;// -1.5;
		AccelX = 0;// -1.5;
	}else

	if(MoveDown) {
		AccelY = 0.25;// 1.5;
		AccelX = 0;// -1.5;
	}
	SpeedX += AccelX * CFPS::FPSControl.GetSpeedFactor();
	SpeedY += AccelY * CFPS::FPSControl.GetSpeedFactor();

	if(SpeedX > MaxSpeedX)  SpeedX =  MaxSpeedX;
	if(SpeedX < -MaxSpeedX) SpeedX = -MaxSpeedX;
	if(SpeedY > MaxSpeedY)  SpeedY =  MaxSpeedY;
	if(SpeedY < -MaxSpeedY) SpeedY = -MaxSpeedY;
	OnAnimate();
	OnMove(SpeedX, SpeedY);
}

//-----------------------------------------------------------------------------
void CZombieRojo::OnRender(SDL_Surface* Surf_Display) {
	CEnemigo::OnRender(Surf_Display);
}

//------------------------------------------------------------------------------
void CZombieRojo::OnCleanup() {
	CEnemigo::OnCleanup();
}
//------------------------------------------------------------------------------
void CZombieRojo::OnAnimate() {
	if(SpeedX !=0 || SpeedY!=0) {
		Anim_Control.MaxFrames = 6;
	}else{
		Anim_Control.MaxFrames = 0;
	}

	CEnemigo::OnAnimate();
}

//------------------------------------------------------------------------------
bool CZombieRojo::OnCollision(CObjetoAnimado* Objeto) {
    if (Objeto->Type == ENTITY_TYPE_EXPLOSION ||
        Objeto->Type == ENTITY_TYPE_BALA)
        Inactive=true;
    return true;
}

//=============================================================================
void CZombieRojo::OnMove(float MoveX, float MoveY) {
	if (MoveX == 0 && MoveY == 0) return;

	double NewX = 0;
	double NewY = 0;

	//CanJump = false;

	MoveX *= CFPS::FPSControl.GetSpeedFactor();
	MoveY *= CFPS::FPSControl.GetSpeedFactor();

	if(MoveX != 0) {
		if(MoveX >= 0) 	NewX =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewX = -CFPS::FPSControl.GetSpeedFactor();
	}
    else
	if(MoveY != 0) {
		if(MoveY >= 0) 	NewY =  CFPS::FPSControl.GetSpeedFactor();
		else 			NewY = -CFPS::FPSControl.GetSpeedFactor();
	}

	while(true) {
		if(Flags & ENTITY_FLAG_GHOST) {
			PosValid((int)(X + NewX), (int)(Y + NewY)); //We don't care about collisions, but we need to send events to other entities

			X += NewX;
			Y += NewY;
		}else{
			if(PosValid((int)(X + NewX), (int)(Y))) {
				X += NewX;
			}else{
				//SpeedX = (-1)*SpeedX;
				//MoveLeft=!MoveLeft;
				//MoveRight=!MoveRight;
				//this->ObtenerDireccion();
				SpeedX=0;
                SpeedY=0;
                this->ObtenerDireccion();
			}

			if(PosValid((int)(X), (int)(Y + NewY))) {
				Y += NewY;
			}else{
               /* if(MoveY > 0) {
                    //CanJump = true;
                }*/
				SpeedY = (-1)*SpeedY;
				MoveDown=!MoveDown;
				MoveUp=!MoveUp;
				//this->ObtenerDireccion();
			}
		}

		MoveX += -NewX;
		MoveY += -NewY;

		if(NewX > 0 && MoveX <= 0) NewX = 0;
		if(NewX < 0 && MoveX >= 0) NewX = 0;

		if(NewY > 0 && MoveY <= 0) NewY = 0;
		if(NewY < 0 && MoveY >= 0) NewY = 0;

		if(MoveX == 0) NewX = 0;
		if(MoveY == 0) NewY = 0;

		if(MoveX == 0 && MoveY 	== 0) 	break;
		if(NewX  == 0 && NewY 	== 0) 	break;
	}
}
